//
//  MessageViewController.m
//  WashCar
//
//  Created by 张越 on 16/3/16.
//  Copyright © 2016年 张越. All rights reserved.
//

#import "MessageViewController.h"
#import "MessageCell.h"
#import "MessageModel.h"
#import "MessageCell.h"
#import "MBProgressHUD+Add.h"

@interface MessageViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic,assign)NSInteger page;
@property (strong, nonatomic) IBOutlet UIImageView *nothingImage;
@property (strong, nonatomic) IBOutlet UILabel *nothingLabel;

@end

@implementation MessageViewController
- (NSMutableArray *)dataSource
{
    if (!_dataSource) {
        self.dataSource = [NSMutableArray new];
    }
    return _dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"消息";
    __weak typeof(self)weakSelf = self;
    [self addLeftButtonWithImage:[UIImage imageNamed:@"返回箭头"] actionBlock:^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
    [self.tableView registerNib:[UINib nibWithNibName:@"MessageCell" bundle:nil] forCellReuseIdentifier:@"MessageCell"];
    self.tableView.tableFooterView = [UIView new];
    self.page = 1;
    [self webRequest_GetMessage];
    // 添加上拉刷新控件
    [self.tableView addFooterWithTarget:self action:@selector(loadMoreData)];
    
    // 添加下拉刷新控件
    [self.tableView addHeaderWithTarget:self action:@selector(loadOnePageData)];
}

- (void)loadMoreData
{
    self.page++;
    [self webRequest_GetMessage];
}

- (void)loadOnePageData
{
    self.page = 1;
    [self webRequest_GetMessage];
}

#pragma mark -- 网络请求 --
-(void)webRequest_GetMessage
{
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    hud.labelText = @"正在加载...";
    [self.view addSubview:hud];
    [hud show:YES];
    __weak typeof(self)weakself = self;
    NSString *uidStr = [[NSUserDefaults standardUserDefaults] objectForKey:KUserDefaultsUserIdKey];
    NSString *page = [NSString stringWithFormat:@"%ld",self.page];
    NSDictionary *dict = @{@"page": page,@"uid":uidStr};
    [HttpRequestManager getMethod:KSystemMessageInterface parameters:dict completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (success) {
            if (self.page == 1) {
                [self.dataSource removeAllObjects];
            }
            self.dataSource = nil;
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            NSArray *ary = response[@"data"];
            for (NSDictionary *dic in ary) {
                MessageModel *model = [MTLJSONAdapter modelOfClass:[MessageModel class] fromJSONDictionary:dic error:nil];
                [weakself.dataSource addObject:model];
            }
            [weakself.tableView reloadData];
            
        }else{
            
        }
        [weakself.tableView footerEndRefreshing];
        [weakself.tableView headerEndRefreshing];
    }];
}

#pragma mark ---tableViewDelegate---
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.dataSource.count > 0) {
        self.nothingImage.hidden = self.nothingLabel.hidden = YES;
    }else{
        self.nothingImage.hidden = self.nothingLabel.hidden = NO;
    }
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MessageCell" forIndexPath:indexPath];
    MessageModel *model = self.dataSource[indexPath.row];
    cell.messageModel = model;
    if(indexPath.row % 2 == 0)
    {
        cell.BackgroundView.backgroundColor = [UIColor colorFromHexCode:@"e5ecf4"];
    }else{
        cell.BackgroundView.backgroundColor = [UIColor colorFromHexCode:@"8fcaf2"];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 取消选中状态
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

//返回cell的高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MessageModel *model = self.dataSource[indexPath.row];
    return [MessageCell cellHeight:model];
    return 190;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
